﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.SessionState;

namespace TransFormer.Web.admin.AdminAshx
{
    /// <summary>
    /// ProductRemove 的摘要说明
    /// </summary>
    public class ProductRemove : IHttpHandler, IRequiresSessionState
    {
        private log4net.ILog _logger = log4net.LogManager.GetLogger(typeof(ProductRemove));
        private BLL.Product _bll = new BLL.Product();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var ids = context.Request["ids"];
            if (string.IsNullOrEmpty(ids)) return;
            if (Regex.IsMatch(ids, @"[^\d,]+")) return;
            var list = _bll.GetModelList("productId in (" + ids + ")");
            if (_bll.DeleteList(ids))
            {
                foreach (var model in list)
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(model.PicUrl))
                        {
                            string path = context.Server.MapPath("." + model.PicUrl);
                            File.Delete(path);
                        }
                        if (!string.IsNullOrEmpty(model.Size))
                        {
                            string path = context.Server.MapPath("." + model.Size);
                            File.Delete(path);
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.Error("删除产品图片失败！", e);
                    }

                }
                context.Response.Write("true");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}